package com.prs.crm.action.online;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.online.Vote;
import com.prs.crm.domain.online.VoteItem;
import com.prs.crm.service.online.VoteItemService;
import com.prs.crm.service.online.VoteService;

public class VoteAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Vote vote;
	private VoteService voteService;
	private PaginationSupport pagination;
	private File upload;
	private String uploadFileName;
	private VoteItem vo;
	private VoteItemService voteItemService;
	private List<VoteItem> vi; 
	private Integer[] vos;

	public Integer[] getVos() {
		return vos;
	}
	public void setVos(Integer[] vos) {
		this.vos = vos;
	}
	public List<VoteItem> getVi() {
		return vi;
	}
	public void setVi(List<VoteItem> vi) {
		this.vi = vi;
	}
	public VoteItem getVo() {
		return vo;
	}
	public void setVo(VoteItem vo) {
		this.vo = vo;
	}
	public VoteItemService getVoteItemService() {
		return voteItemService;
	}
	public void setVoteItemService(VoteItemService voteItemService) {
		this.voteItemService = voteItemService;
	}
	public Vote getVote() {
		return vote;
	}
	public void setVote(Vote vote) {
		this.vote = vote;
	}
	public VoteService getVoteService() {
		return voteService;
	}
	public void setVoteService(VoteService voteService) {
		this.voteService = voteService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	@Action("listVote")
	public String listVote(){
		List<Vote> vot=this.getVoteService().getAllVote();
		for (Vote vos : vot) {
			if (vos.getLimitDate()!=null) {
				if (new Date().after(vos.getLimitDate())) {
					vos.setShowVote("过期");
					this.getVoteService().submit(vos);
				}else{
					vos.setShowVote("未过期");
					this.getVoteService().submit(vos);
				}
			}
			String[] subPerson=vos.getVotePerson().split(",");
			for (String sub : subPerson) {
				if (AppContext.getAccountContext().currentUser().getResume().getName().equals(sub)) {
					vos.setShouSubmit("提交过");
					this.getVoteService().submit(vos);
					break;
				}else{
						vos.setShouSubmit("未提交过");
						this.getVoteService().submit(vos);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Vote.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getVoteService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editVote")
	public String editVote(){
		return SUCCESS;
	}
	@Action("viewVote")
	public String viewVote(){
		vi=this.vote.getVoteItem();
		if(vote!=null){
			vote.setReadyNum(vote.getReadyNum()+1);
			
			if (vote.getLimitDate()!=null) {
				if (new Date().after(vote.getLimitDate())) {
					vote.setShowVote("过期");
					this.getVoteService().submit(vote);
				}else{
					vote.setShowVote("未过期");
					this.getVoteService().submit(vote);
				}
			}
		}	
		return SUCCESS;
	}
	@Action(value = "saveVote", results = {
			@Result(name = "success", type = "redirect", location = "viewVote?vote=${vote.getId()}"),
			@Result(name = "error", type = "chain", location = "editVote")})
	public String saveVote(){
		if(upload!=null){
			this.vote.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}	
		vote.setState(0);
		Person person=new Person();
		person=AppContext.getAccountContext().currentUser();
		vote.setLastPerson(person.getResume().getName());
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		vote.setLastTime(sf.format(dt));
		if (vote.getLimitDate()!=null) {
			if (new Date().after(vote.getLimitDate())) {
				vote.setShowVote("过期");
			}else{
				vote.setShowVote("未过期");
			}
		}
		if (vote.getId()==null) {
			if (vote.getRemark()==null || "".equals(vote.getRemark().trim())) {
				this.addActionError("投票选项不能为空");
				return ERROR;
			}
			vote.setVotePerson("");
			vote.setReadyNum(0);
			vote.setVoteNum(0);
			this.getVoteService().save(vote);
		}else{
			if (vote.getRemark()==null || "".equals(vote.getRemark().trim())) {
				vote.setVotePerson(vote.getVotePerson());
			}
				this.getVoteService().submit(vote);
		}
		System.out.println(vote.getRemark());
		String[] a=vote.getRemark().split("[\\t \\n]+");
		for (String aa : a) {
			System.out.println(aa);
				vo=new VoteItem();
				vo.setName(aa);
				vo.setVote(vote);
				vo.setVoteNum(0);
				this.getVoteItemService().save(vo);	
				if (vote.getRemark()==null || "".equals(vote.getRemark().trim())){
					this.getVoteItemService().remove(vo);
				}
		}
		return SUCCESS;
	}
	@Action(value = "saveVotes", results = {
			@Result(name = "success", type = "redirect", location = "viewVote?vote=${vote.getId()}"),
			@Result(name = "error", type = "chain", location = "viewVote?vote=${vote.getId()}")})
	public String saveVotes(){
//		if (vo==null) {
//			this.addActionError("请选择一行投票再进行投票");
//			return ERROR;
//		}
		Person person=AppContext.getAccountContext().currentUser();
		vote.setVoteNum(vote.getVoteNum()+1);
		vote.setLastPerson(person.getResume().getName());
		if (vote.getVotePerson()==null || "".equals(vote.getVotePerson().trim())) {
			vote.setVotePerson(person.getResume().getName());
		}else{
			vote.setVotePerson(vote.getVotePerson()+","+person.getResume().getName());
		}
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		vote.setLastTime(sf.format(dt));
		String[] subPerson=vote.getVotePerson().split(",");
		for (String sub : subPerson) {
			if (AppContext.getAccountContext().currentUser().getResume().getName().equals(sub)) {
				vote.setShouSubmit("提交过");
				break;
			}else{
				vote.setShouSubmit("未提交过");
			}
		}
		this.getVoteService().submit(vote);
		if (vote.getOptions().equals("单选投票")) {
			VoteItem vit=this.getVoteItemService().getVoteItems(vo.getId());
			System.out.println(vit.getId());
			if (vit!=null) {
				if (vit.getVotePerson()==null || "".equals(vit.getVotePerson().trim())) {
					vit.setVotePerson(person.getResume().getName());
				}else{
					vit.setVotePerson(vit.getVotePerson()+","+person.getResume().getName());
				}
				
				vit.setVoteNum(vit.getVoteNum()+1);
				vit.setVote(vote);
				this.getVoteItemService().submit(vit);
			}
		}else if(vote.getOptions().equals("多选投票")){
			int len=vos.length;
			for (int i = 0; i < len; i++) {
				VoteItem vot=this.getVoteItemService().getVoteItems(vos[i]);
				if (vot!=null) {
					if (vot.getVotePerson()==null || "".equals(vot.getVotePerson().trim())) {
						vot.setVotePerson(person.getResume().getName());
					}else{
						vot.setVotePerson(vot.getVotePerson()+","+person.getResume().getName());
					}
					
					vot.setVoteNum(vot.getVoteNum()+1);
					vot.setVote(vote);
					this.getVoteItemService().submit(vot);
				}
			}
		}
		return SUCCESS;
	}
	@Action(value = "saveVoteLock", results = {
			@Result(name = "success", type = "redirect", location = "viewVote?vote=${vote.getId()}"),
			@Result(name = "error", type = "chain", location = "viewVote?vote=${vote.getId()}")})
	public String saveVoteLock(){
		vote.setState(1);
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		vote.setDate(sf.format(dt));
		this.getVoteService().save(vote);
		return SUCCESS;
	}
	@Action(value = "saveVoteLocks", results = {
			@Result(name = "success", type = "redirect", location = "viewVote?vote=${vote.getId()}"),
			@Result(name = "error", type = "chain", location = "viewVote?vote=${vote.getId()}")})
	public String saveVoteLocks(){
		vote.setState(0);
		this.getVoteService().save(vote);
		return SUCCESS;
	}
	@Action(value = "removeVote", results = {
			@Result(name = "success", type = "redirect", location = "listVote"),
			@Result(name = "error", type = "chain", location = "viewVote?vote=${vote.getId()}")})
	public String removeVote(){
		 this.getVoteService().remove(vote);
		return SUCCESS;
	}
}
